package com.sydata.job;

import com.sydata.cdrdata.domain.LinksFieldCdrRecord;
import com.sydata.cdrdata.mapper.LinksFieldCdrMapper;
import com.sydata.cdrdata.service.ILkCdrService;
import com.sydata.service.SftpService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Async;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

import java.text.SimpleDateFormat;
import java.util.*;


/**
 * 每天中午1点获取领科CRD数据 汇总整理入库
 *
 * @author huangzj@uhuibao.com
 */
@Component
public class CdrSchedule {

    public static Logger logger = LoggerFactory.getLogger(CdrSchedule.class);


    @Autowired
    private SftpService sftpService;

    @Autowired
    private ILkCdrService lkCdrService;

    @Autowired
    LinksFieldCdrMapper linksFieldCdrMapper;

    @Async
    @Scheduled(cron = "0 30 17 * * ?") // 每天17点30钟执行
    public void process() {

        // 创建日期格式化对象
        SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMdd");
        // 获取当前日期
        Date date = new Date();
        // 格式化日期
        String formattedDate = sdf.format(date);
        logger.info("【开始】" + formattedDate + " 话单数据读取汇总");


        List<String> lines = sftpService.connAndReadLinesFromFile(formattedDate);
        if (!lines.isEmpty()) {
            logger.info("【文件共有】{} 条件数据 ", lines.size() - 1);

            //初始化insert 的 list  批量插入
            List<LinksFieldCdrRecord> cdrList = new ArrayList<>();
            for (int i = 1; i < lines.size(); i++) {
                String line = lines.get(i);
                String[] values = line.split(",");

                String imsi = values[3];
                String mcc = values[4];
                String mnc = values[5];
                Long flow = Long.valueOf(values[6]);
                String billDate = values[7];
                String endDate = values[8];

                //关联查询 获取ICCID、渠道ID、代理商ID 等信息
                String iccid = "";
                String packageId = "";
                String packageCode = "";
                String packageName = "";
                long channelId = 0L;
                long agentId = 0L;
                List<Map<String, Object>> imsiInfo = lkCdrService.getImsiInfo(imsi, billDate);
                if (!imsiInfo.isEmpty()) {
                    Map<String, Object> firstRecord = imsiInfo.get(0);
                    iccid = firstRecord.get("ICCID").toString();
                    channelId = Long.parseLong(firstRecord.get("CHANNELID").toString());
                    agentId = Long.parseLong(firstRecord.get("AGENTID").toString());

                    packageId = firstRecord.get("PACKAGE_ID").toString();
                    packageCode = firstRecord.get("PACKAGE_CODE").toString();
                    packageName = firstRecord.get("PACKAGE_NAME").toString();
                }

                LinksFieldCdrRecord cdrRecord = new LinksFieldCdrRecord();
//                long cdrRecordId = linksFieldCdrMapper.selectLkCdrSeqId();
//                cdrRecord.setRecordId(cdrRecordId);
                cdrRecord.setChannelId(channelId);
                cdrRecord.setAgentId(agentId);
                cdrRecord.setBillDate(billDate);
                cdrRecord.setEndDate(endDate);
                cdrRecord.setIccid(iccid);
                cdrRecord.setImsi(imsi);
                cdrRecord.setMcc(mcc);
                cdrRecord.setMnc(mnc);
                cdrRecord.setFlow(flow);
                cdrRecord.setRecordFile(formattedDate);
                cdrRecord.setPackageId(packageId);
                cdrRecord.setPackageCode(packageCode);
                cdrRecord.setPackageName(packageName);
                // 添加用户对象到 records 列表中
                lkCdrService.insert(cdrRecord);

            }

            // 添加更多的用户对象到 records 列表中
//            lkCdrService.insertBatch(cdrList);
            logger.info("【结束】" + formattedDate + " 话单数据读取汇总");
        }

    }

}
